﻿using MySqlConnector;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using SqlSugar;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;

namespace Tgather
{
    public class SqlSugarHelper
    {
        static Tgather.Config config = new Tgather.Config();

        static string connectionString = string.Format("Database={0};Host ={1};Port={2};Username={3};Password={4};SslMode=none;AllowPublicKeyRetrieval=True", config.DBDatabase,config.DBHost,config.DBPort,config.DBUser,config.DBPwd);

        static Logger log = new Logger("log");

        public SqlSugarScope Db = new SqlSugarScope(new ConnectionConfig()
        {
            
            ConnectionString = connectionString,//连接符字串
            DbType = DbType.MySql,//数据库类型
            IsAutoCloseConnection = true //不设成true要手动close
        },
            db => {
                db.Aop.OnLogExecuting = (sql, pars) =>
                {
                    Console.WriteLine(sql);//输出sql,查看执行sql 性能无影响


                };
                db.Aop.OnError = (exp) =>//SQL报错
                {
                    
                    log.Error(exp.ToString());
                };
            }
           );
        public bool state()
        {
            Debug.WriteLine(connectionString);
            return Db.Ado.IsValidConnection();
        }

        public int insert(Object obj)
        { 
            return Db.InsertableByObject(obj).ExecuteCommand();
        }

        /// <summary>数量
        /// 
        /// </summary>
        /// <param name="paramaters"></param>
        /// <returns></returns>
        public int count(Dictionary<string, Object> paramaters)
        {
            int count = Db.Queryable<Project>().WhereColumns(paramaters).Count();
            return count;
        }

        /// <summary> 转换查询条件
        /// 
        /// </summary>
        /// <param name="paramaters"></param>
        public List<IConditionalModel> dictionaryToConditional(Dictionary<string, Object> paramaters)
        {
            Dictionary<string, Object> obj = new Dictionary<string, object>();
            IEnumerable<string> keys = paramaters.Keys;
            List<Dictionary<string, Object>> parameters = new List<Dictionary<string, Object>>();
            // 输出所有的key
            foreach (var key in keys)
            {
                obj["FieldName"] = key;
                obj["ConditionalType"] = "0";
                obj["FieldValue"] = paramaters[key];
                parameters.Add(obj);
            }

            string strjson = JsonConvert.SerializeObject(parameters);

            return Db.Utilities.JsonToConditionalModels(strjson);
        }
    }

    [SugarTable("t_project")]
    public class Project
    {
        //数据是自增需要加上IsIdentity 
        //数据库是主键需要加上IsPrimaryKey 
        //注意：要完全和数据库一致2个属性
        [SugarColumn(IsPrimaryKey = true, IsIdentity = false)]
        public string p_id { get; set; }
        public string? p_remote_id { get; set; }
        public DateTime start_time { get; set; }
        public DateTime end_time{ get; set; }
        public string? unit { get; set; }
        public string? construction_type { get; set; }
        public string? p_name { get; set; }
        public string? p_charge { get; set; }
        public string? memo { get; set; }
        public DateTime create_time { get; set; }
        public int? report_status { get; set; }
    }

    [SugarTable("t_project_id")]
    public class ProjectID
    {
        //数据是自增需要加上IsIdentity 
        //数据库是主键需要加上IsPrimaryKey 
        //注意：要完全和数据库一致2个属性
        [SugarColumn(IsPrimaryKey = true, IsIdentity = false)]
        public string? p_id { get; set; }
        public string? p_remote_id { get; set; }
 
    }

    [SugarTable("t_point")]
    public class PPoint
    {
        //数据是自增需要加上IsIdentity 
        //数据库是主键需要加上IsPrimaryKey 
        //注意：要完全和数据库一致2个属性
        [SugarColumn(IsPrimaryKey = true, IsIdentity = false)]
        public string? p_id { get; set; }
        public float inlet_pressure { get; set; }
        public float inlet_temperature { get; set; }
        public float inlet_instantaneous_flow { get; set; }
        public float inlet_cumulative_flow { get; set; }
        public float outlet_pressure { get; set; }
        public float outlet_temperature { get; set; }
        public float outlet_instantaneous_flow { get; set; }
        public float outlet_cumulative_flow { get; set; }
        public float box_temperature { get; set; }
        public string? create_time { get; set; }
    }
}
